package com.Bigbluebook.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.Bigbluebook.Activity.PostDetailsActivity;
import com.Bigbluebook.R;
import com.Bigbluebook.Service.CommentActionListener;
import com.Bigbluebook.entity.Comment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {
    private List<Comment> commentList = new ArrayList<>();;
    private CommentActionListener commentActionListener;
    public void setCommentsList(List<Comment> comments) {
        this.commentList = comments;
        notifyDataSetChanged();
    }

    public CommentAdapter(List<Comment> commentsList, CommentActionListener commentActionListener) {
        this.commentList = commentsList;
        this.commentActionListener = commentActionListener; // 接收接口实例
    }

    @NonNull
    @Override
    public CommentAdapter.CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_section,parent,false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentAdapter.CommentViewHolder holder, int position) {
        Comment comment = commentList.get(position);
        holder.user.setText("Comment by: " + comment.getUser().getUsername());
        holder.content.setText(comment.getContent());
        holder.time.setText(formatDate(comment.getCreatedAt()));

        //点赞评论按钮点击事件
        holder.btnLikeComment.setOnClickListener(v -> {
           if(commentActionListener != null){
               commentActionListener.onLikeComment(comment);
           }
        });

        //回复评论按钮点击事件
        holder.btnReplyComment.setOnClickListener(v -> {
           if(commentActionListener != null){
               commentActionListener.onReplyToComment(comment);
           }
        });
    }

    @Override
    public int getItemCount() {
        if(commentList==null){
            return 0;
        }
        return commentList.size();
    }

    static class CommentViewHolder extends RecyclerView.ViewHolder {
        TextView user;
        TextView content;
        TextView time;
        ImageButton btnLikeComment;
        ImageButton btnReplyComment;

        public CommentViewHolder(View itemView) {
            super(itemView);
            user = itemView.findViewById(R.id.CommentUser);
            content = itemView.findViewById(R.id.CommentContent);
            time = itemView.findViewById(R.id.CommentTime);
            btnLikeComment = itemView.findViewById(R.id.btnLikeComment);
            btnReplyComment = itemView.findViewById(R.id.btnReplyComment);
        }
    }

    public String formatDate(Date date) {
        if (date != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            return dateFormat.format(date);
        }
        return "";
    }


}
